# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: jshin

from coin.exchange.shared.kr_rest.product import SharedProduct


def assert_same_rep_currency(x, y):
  assert x.rep_currency == y.rep_currency, (str(x), str(y))


class NativeProducts(object):
  def __init__(self, products, base_currencies, quote_currencies):
    assert len(products) > 0, products
    assert len(base_currencies) > 0, base_currencies
    if type(quote_currencies) != list:
      quote_currencies = [quote_currencies]
    assert len(quote_currencies) > 0, quote_currencies
    # assert type

    self.products = products
    self.base_currencies = base_currencies
    self.quote_currency = quote_currencies[0] if len(quote_currencies) == 1 else None
    self.quote_currencies = quote_currencies
    self.currencies = list(set(self.base_currencies + self.quote_currencies))

  @staticmethod
  def FromSharedProduct(quote_currency_shared, products_shared, ProductType):
    products = []
    base_currencies = []
    quote_currency = ProductType.NativeCurrencyType.FromCurrency(quote_currency_shared)

    assert len(products_shared) > 0
    for product in products_shared:
      product = ProductType.FromProduct(product)
      products.append(product)
      base_currencies.append(product.base)
      assert_same_rep_currency(product.quote, quote_currency)

    return NativeProducts(products, base_currencies, quote_currency)

  @staticmethod
  def FromSharedCurrency(quote_currency_shared, base_currencies_shared, ProductType):
    products = []
    base_currencies = []
    quote_currency = ProductType.NativeCurrencyType.FromCurrency(quote_currency_shared)

    assert len(base_currencies_shared) > 0
    for base_currency in base_currencies_shared:
      product = ProductType.FromStrPair(base_currency.currency, quote_currency.currency)
      products.append(product)
      base_currencies.append(product.base)
      assert_same_rep_currency(product.quote, quote_currency)

    return NativeProducts(products, base_currencies, quote_currency)

  @staticmethod
  def FromTypedStrProduct(quote_currency_str, products_str, ProductType):
    products = []
    base_currencies = []
    quote_currency = ProductType.NativeCurrencyType.FromStr(quote_currency_str)

    assert len(products_str) > 0
    for product_str in products_str:
      product = ProductType.FromStr(product_str)
      products.append(product)
      base_currencies.append(product.base)
      assert_same_rep_currency(product.quote, quote_currency)

    return NativeProducts(products, base_currencies, quote_currency)

  @staticmethod
  def FromTypedStrCurrency(quote_currency_str, base_currencies_str, ProductType):
    products = []
    base_currencies = []
    quote_currency = ProductType.NativeCurrencyType.FromStr(quote_currency_str)

    assert len(base_currencies_str) > 0
    for base_currency in base_currencies_str:
      product = ProductType.FromStrPair(base_currency, quote_currency_str)
      products.append(product)
      base_currencies.append(product.base)
      assert_same_rep_currency(product.quote, quote_currency)

    return NativeProducts(products, base_currencies, quote_currency)

  @staticmethod
  def FromSharedStrProduct(quote_currency_str, products_str):
    return NativeProducts.FromTypedStrProduct(quote_currency_str, products_str, SharedProduct)

  @staticmethod
  def FromSharedStrCurrency(quote_currency_str, base_currencies_str):
    return NativeProducts.FromTypedStrCurrency(quote_currency_str,
                                               base_currencies_str,
                                               SharedProduct)
